<?php
App::import('Model', 'news.News');
App::import('Model', 'news.NewsCategory');

class SectionsController extends AppController {
	var $name = 'Sections';
	var $uses = array('ExtraNewsField');
	var $helpers = array();
	/**
	 @var $model
	 @brief Thông tin về model, được sử dụng trong trường hợp override thông tin của danh mục
	 */
	var $models = array(
		'Category'	=>	array(
			'plugin'	=>	'news',
			'name'		=>	'NewsCategory'
		),
		'Page'		=>	array(
			'plugin'	=>	'news',
			'name'		=>	'News'
		)
	);
	function beforeFilter() {
		parent::beforeFilter();
		foreach ($this->models as $key => $item) {
			App::import('Model', $item['plugin'].".".$item['name']);
			$this->loadModel($item['name']);
		}
		
		$this->set('models', $this->models);
		
		$this->configs = $this->{$this->models['Page']['name']}->loadConfig();
		$this->configs = $this->{$this->models['Page']['name']}->mergeConfig($this->configs, array(
			'configFolder'		=>	'news',
			'names'				=> array($this->models['Page']['name'], 'Category', 'Appearence', 'ExtraFields')
		));
		
		$this->set('configs', $this->configs);
		
		$this->{$this->models['Page']['name']}->bindModel(array(
			'hasOne'	=> array(
				"{$this->ExtraNewsField->name}" => array(
					"className" 	=> "{$this->ExtraNewsField->name}",
					"foreignKey"	=> "foreign_id"
				)
			)
		), false);
	}
	function index($alias) {
		// get top news
		$top_news = $this->{$this->models['Page']['name']}->find('all', array(
			'conditions'	=>	array(
				'Category.alias'	=> $alias
			),
			'contain'	=>	array('ExtraNewsField'),
			'fields'	=>	array('id','title','image','description','alias','Category.alias', 'Category.name'),
			'joins' => array(
				array(
					'table' => $this->{$this->models['Page']['name']}->hasAndBelongsToMany['Category']['joinTable'],
					'type' => 'INNER',
					'alias' => 'ReferenceCategory',
					'conditions' => array(
						"News.id = ReferenceCategory.page_id"
					)
				),
				array(
					'table' => $this->{$this->models['Category']['name']}->table,
					'type' => 'INNER',
					'alias' => 'Category',
					'conditions' => array(
						'Category.id = ReferenceCategory.category_id'
					)
				)
			),
			'order'	=>	array('ExtraNewsField.created_date desc'),
			'limit'	=>	$this->configs[$this->models['Page']['name']]['Section']['top']
		));
		//debug($top_news);exit;
		$this->set('top_news', $top_news);
		
		$this->paginate[$this->models['Page']['name']] = array(
			'conditions'	=>	array(
				'Category.alias'	=> $alias
			),
			'contain'	=>	array('ExtraNewsField'),
			'fields'	=>	array('id','title','alias'),
			'joins' => array(
				array(
					'table' => $this->{$this->models['Page']['name']}->hasAndBelongsToMany['Category']['joinTable'],
					'type' => 'INNER',
					'alias' => 'ReferenceCategory',
					'conditions' => array(
						"News.id = ReferenceCategory.page_id"
					)
				),
				array(
					'table' => $this->{$this->models['Category']['name']}->table,
					'type' => 'INNER',
					'alias' => 'Category',
					'conditions' => array(
						'Category.id = ReferenceCategory.category_id'
					)
				)
			),
			'order'	=>	array('ExtraNewsField.created_date desc'),
			'limit'	=>	$this->configs[$this->models['Page']['name']]['Section']['other_news'],
			'offset'	=>	$this->configs[$this->models['Page']['name']]['Section']['top']
			
		);
		
		$other_news = $this->paginate($this->models['Page']['name']);
		
		$this->set('other_news', $other_news);
	}
	
	function get_other_news($alias) {
		$this->paginate[$this->models['Page']['name']] = array(
			'conditions'	=>	array(
				'Category.alias'	=> $alias
			),
			'contain'	=>	array('ExtraNewsField'),
			'fields'	=>	array('id','title','alias'),
			'joins' => array(
				array(
					'table' => $this->{$this->models['Page']['name']}->hasAndBelongsToMany['Category']['joinTable'],
					'type' => 'INNER',
					'alias' => 'ReferenceCategory',
					'conditions' => array(
						"News.id = ReferenceCategory.page_id"
					)
				),
				array(
					'table' => $this->{$this->models['Category']['name']}->table,
					'type' => 'INNER',
					'alias' => 'Category',
					'conditions' => array(
						'Category.id = ReferenceCategory.category_id'
					)
				)
			),
			'order'	=>	array('ExtraNewsField.created_date desc'),
			'limit'	=>	$this->configs[$this->models['Page']['name']]['Section']['other_news'],
			'offset'	=>	$this->configs[$this->models['Page']['name']]['Section']['top']
			
		);
		
		$other_news = $this->paginate($this->models['Page']['name']);
		
		$this->set('other_news', $other_news);
	}
}
?>